package com.shopping.member.controller;

import com.alibaba.fastjson.JSON;
import com.shopping.common.exception.BizCodeEnum;
import com.shopping.common.utils.PageUtils;
import com.shopping.common.utils.R;
import com.shopping.member.entity.MemberEntity;
import com.shopping.member.exception.PhoneExistException;
import com.shopping.member.exception.UsernameExistException;
import com.shopping.member.feign.CouponFeignServer;
import com.shopping.member.service.MemberService;
import com.shopping.member.vo.MemberLoginVo;
import com.shopping.member.vo.MemberRegisterVo;
import com.shopping.member.vo.SocialUserVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 会员
 *
 * @author Europa
 * @email qw1124sdo8@live.com
 * @date 2020-04-12 01:16:39
 */
@RestController
@RequestMapping("member/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    @Resource
    private CouponFeignServer couponFeignServer;

    @RequestMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setUsername("张三");
        R memberCoupons = couponFeignServer.memberCoupons();
        return R.ok().put("member", memberEntity).put("coupons", memberCoupons.get("coupons"));
    }

    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUserVo userVo) {
        MemberEntity memberEntity = memberService.oauthLogin(userVo);
        System.out.println("memberEntity = " + memberEntity);
        if (memberEntity == null) {
            int code = BizCodeEnum.LOGIN_VERIFY_EXCEPTION.getCode();
            String msg = BizCodeEnum.LOGIN_VERIFY_EXCEPTION.getMsg();
            return R.error(code, msg);
        }
        String json = JSON.toJSONString(memberEntity);
        return R.ok(json);
    }


    @RequestMapping("/register")
    public R register(@RequestBody MemberRegisterVo registerVo) {
        try {
            memberService.register(registerVo);
        } catch (PhoneExistException e) {
            int code = BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode();
            String msg = BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg();
            return R.error(code, msg);
        } catch (UsernameExistException e) {
            int code = BizCodeEnum.USER_EXIST_EXCEPTION.getCode();
            String msg = BizCodeEnum.USER_EXIST_EXCEPTION.getMsg();
            return R.error(code, msg);
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo loginVo) {
        MemberEntity memberEntity = memberService.login(loginVo);
        if (memberEntity == null) {
            int code = BizCodeEnum.LOGIN_VERIFY_EXCEPTION.getCode();
            String msg = BizCodeEnum.LOGIN_VERIFY_EXCEPTION.getMsg();
            return R.error(code, msg);
        }
        String json = JSON.toJSONString(memberEntity);
        return R.ok(json);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
